const request = require('request');
const fs = require('fs');

request.get('https://huaban.com/discovery/beauty/', (err, resp, body) => {
    const startIndex = /\[\"pins\"\]\ =\ /.exec(body).index + 11;
    const endIndex = /}}];/.exec(body.slice(startIndex)).index + startIndex + 3;

    const jsonStr = body.slice(startIndex, endIndex);
    const jsonObj = JSON.parse(jsonStr);
    
    jsonObj.forEach(item => {
        const url = 'https://hbimg.huabanimg.com/' + item['file']['key'];
        request.get(url).pipe(fs.createWriteStream(item['file']['key'] + '.jpg'));
    });
});

